package com.qth.valet.mp;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import org.apache.ibatis.type.JdbcType;

import java.util.Collections;

public class MyBatisPlusGenerator {

    public static void main(String[] args) {

        final String serverInfo = "120.24.195.55:3306";
        final String userName = "root2";
        final String password = "123456";
        final String databaseName = "qth";
        final String databaseUrl = "jdbc:mysql://" + serverInfo + "/" + databaseName +
                "?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&tinyInt1isBit=true";

        final String targetModuleNamePath = System.getProperty("user.dir");

//        final String[] tables = { "coupon", "coupon_user" };
//        final String[] tables = { "payment" };
//        final String[] tables = { "driver_recharge" };
//        final String[] tables = { "driver_balance_detail" };
//        final String[] tables = { "property" };
//        final String[] tables = { "notice_driver" };
        final String[] tables = { };

        // 数据源配置
        FastAutoGenerator
                .create(databaseUrl, userName, password)
                .globalConfig(builder -> {
                    builder.author("蔡徐坤")        // 设置作者
                            .commentDate("yyyy-MM-dd")
                            .enableSpringdoc()
                            .disableOpenDir() //禁止打开输出目录
                            .dateType(DateType.ONLY_DATE)   //定义生成的实体类中日期类型 DateType.ONLY_DATE 默认值: DateType.TIME_PACK
                            .outputDir(targetModuleNamePath + "/src/main/java"); // 指定输出目录
                })
                .dataSourceConfig(builder -> {
                    builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                        //Mysql下tinyint字段转换
                        if (JdbcType.TINYINT == metaInfo.getJdbcType()) {
                            return DbColumnType.BOOLEAN;
                        }
                        return typeRegistry.getColumnType(metaInfo);
                    });
                })
                .templateConfig(builder -> {
                    // 设置不生成controller和service
//                    builder.disable(TemplateType.CONTROLLER, TemplateType.SERVICE, TemplateType.SERVICE_IMPL);
                })
                .packageConfig(builder -> {
                    builder.parent("com.qth.valet") // 父包模块名
                            .entity("entity.po")      //Entity 包名 默认值:entity
                            .mapper("mapper")     //Mapper 包名 默认值:mapper
                            .pathInfo(Collections.singletonMap(OutputFile.xml, targetModuleNamePath + "/src/main/resources/mapper")); // 设置mapperXml生成路,默认存放在mapper的xml下
                })

                .strategyConfig(builder -> {
                    // 设置需要生成的表名
                    builder.addInclude(tables);
                    // Entity策略配置
                    builder.entityBuilder()
                            .enableFileOverride()            // 开启生成Entity层文件覆盖
                            .idType(IdType.ASSIGN_ID)        // 主键策略
                            .enableLombok()                  // 开启lombok
                            .logicDeleteColumnName("deleted")// 说明逻辑删除是哪个字段
                            .enableTableFieldAnnotation()    // 属性加上注解说明
                            .formatFileName("%s");
                    // Controller策略配置
                    builder.controllerBuilder()
                            .enableFileOverride()            // 开启生成Controller层文件覆盖
                            .formatFileName("%sController");
                    // Service策略配置
                    builder.serviceBuilder()
                            .enableFileOverride()            // 开启生成Service层文件覆盖
                            .superServiceClass(IService.class)
                            .superServiceImplClass(ServiceImpl.class)
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl");
                    // Mapper策略配置
                    builder.mapperBuilder()
                            .enableFileOverride()            // 开启生成Mapper层文件覆盖
                            .formatMapperFileName("%sMapper")
                            .superClass(BaseMapper.class)
                            .enableBaseResultMap()
                            .enableBaseColumnList()
                            .formatXmlFileName("%sMapper");
                })
                .execute();

    }


}
